import { Component, OnInit, Input, ViewChild,TemplateRef} from '@angular/core';
import { SFSchema, SFComponent } from '@delon/form';
import { HttpService, SupDic, GridComponent } from 'lbf';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { FlowViewComponent } from '../flow-view/flow-view.component';
import { NzAnchorModule } from 'ng-zorro-antd/anchor';
import { ZskService } from 'src/app/routes/tyjb/zskservice/zsk.service';
import { STColumn, STComponent } from '@delon/abc';

@Component({
  selector: 'lb-zskcx',
  templateUrl: './lb_zskcx.component.html',
  styleUrls:['./lb_zskcx.component.less']
})
export class LbZskcxComponent implements OnInit {
  @Input() nzVisible = true;
  @Input() DAD017;
  
  @Input() set ShowLeft(value:boolean) {
    this._showLeft = value
    
  }
  get ShowLeft() {
    return this._showLeft
  } 
  _showLeft = false

  sxData={
    basicInfo:null,
    slqd:[],
    sqtj:null,
    sbcl:[],
    bllc:[],
    sfbz:null,
    cjwt:[],
    sdyj:[],
  };
  /************字典**************** */
  DGB010Map=new Map();
  DGB020Map=new Map();
  DGB040Map=new Map();
  DAD011Map=new Map();
  DAD032Map=new Map();
  DAD011TagMap=new Map();
  DAD032BGMap=new Map();
  DAD040Map=new Map();
  DAD173Map=new Map();
  DAD175Map=new Map();
  /******************材料信息******************* */
  clxxGxVisible=false;
  clxxVisible=false;
  listOfData=[];
  clxxColumns: STColumn[] = [
    { title: '材料名称', index: 'DAD029', fixed: 'left', width: 100 },
    { title: '材料形式', index: 'DAD105',render:'dicDAD034'},
    { title: '纸质材料份数', index: 'DAD120'},
    { title: '材料必要性', index: 'DAD121',render:'dicDAC002'},
    { title: '材料类型', index: 'DAD122',render:'dicDAC003'},
    { title: '纸质材料规格', index: 'DAD123'},
    { title: '渠道来源', index: 'DAD080',render:'dicDAD080'},
    { title: '来源渠道说明', index: 'DAD081'},
    { title: '是否实现共享', index: 'DAD082',render:'dicDAD082'},
    { title: '受理标准', index: 'DAD124'},
    { title: '填报须知', index: 'DAD125'},
    { title: '提供材料依据', index: 'DAD126'},
    { title: '备注', index: 'AAE013'},
  ];
  anchorVisible=false;
  
  isShowLct=false;
  clxxParam;

  /*----------------------------------------------------------------------------------------------------------------------------------- */
  constructor(private lbs:HttpService 
    ,private msgSrv: NzMessageService
    ,private supdic: SupDic,
    private modalService: NzModalService,
    public zskService:ZskService,
    ) {
   }

   public refresh(para:any){
     this.DAD017=para;
    this.initSxData();
   }

  ngOnInit() {
    this.initMap();
    this.initSxData();
  }


  initSxData(){
    this.initSxDataBasicInfo();
    this.initSxDataSlqd();
    this.initSxDataSqtj();
    this.initSxDataSbcl();
    // this.initSxDataBllc();
    this.initSxDataSfbz();
    this.initSxDataCjwt();
    this.initSxDataSdyj();
    this.initClFirst();
  }
  /************************************************************************* */

  initSxDataBasicInfo(){
    this.lbs.lbservice("ZSKWH_QSXJBXX",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.basicInfo=resdata.message.list[0];
        this.anchorVisible=true;
      }else{
        console.log(resdata.errmsg);
      }
    });
  }

  initSxDataSlqd(){
    this.lbs.lbservice("ZSKWH_QSLQDYY",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.slqd=resdata.message.list;
      }else{
        console.log(resdata.errmsg);
      }
    });
  }

  initSxDataSqtj(){
    this.lbs.lbservice("SQTJWH_QSQTJ",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.sqtj=resdata.message.list[0];
        // this.sxData.sqtj.sqtjList=JSON.parse(this.sxData.sqtj.DAD164);
      }else{
        console.log(resdata.errmsg);
      }
    });
  }

  initSxDataSbcl(){
    this.lbs.lbservice("ZSKCX_QSBCL",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.sbcl=resdata.message.list;
        if(this.sxData.sbcl.length>0){
          this.showClSearchBtn=true;
        }
        this.sxData.sbcl.forEach(item=>{
          const sbclCheckGroup=[];
          item.BQ_LIST.forEach(bq_item=>{
            const check_item={ label: '', value: '' }
            check_item.label=bq_item.DAD100;
            check_item.value=bq_item.DAD099;
            sbclCheckGroup.push(check_item);
          });
          item['sbclCheckGroup']=sbclCheckGroup;
        });
      }else{
        console.log(resdata.errmsg);
      }
    });
  }


  // initSxDataBllc(){
  //   this.lbs.lbservice("ZSKWH_QLCSM",{para:{DAD017:this.DAD017}}).then(resdata=>{
  //     if(resdata.code>0){
  //       this.sxData.bllc=resdata.message.list;
  //     }else{
  //       console.log(resdata.errmsg);
  //     }
  //   });
  // }

  initSxDataSfbz(){
    this.lbs.lbservice("SFBZWH_QSFBZ",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.sfbz=resdata.message.list[0];
      }else{
        console.log(resdata.errmsg);
      }
    });
  }

  initSxDataCjwt(){
    this.lbs.lbservice("CJWTWH_QCJWT",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.cjwt=resdata.message.list;
      }else{
        console.log(resdata.errmsg);
      }
    });
  }

  initSxDataSdyj(){
    this.lbs.lbservice("SDYJWH_QSDYJYY",{para:{DAD017:this.DAD017}}).then(resdata=>{
      if(resdata.code>0){
        this.sxData.sdyj=resdata.message.list;
      }else{
        console.log(resdata.errmsg);
      }
    });
  }

  /*********************字典************************** */
  initMap(){
    this.initDGB010Map();
    this.initDGB020Map();
    this.initDGB040Map();
    this.initDAD011Map();
    this.initDAD032Map();
    this.initDAD011TagMap();
    this.initDAD032BGMap();
    this.initDAD040Map();
    this.initDAD173Map();
    this.initDAD175Map();
    // this.initDAD065Map();
    // this.initDAD063Map();
    // this.initDAD012Map();
  }
  initDGB010Map(){
    this.supdic.getSFDic('DGB010').forEach(e=>{
      this.DGB010Map.set(e['value'],e['label']);
    });
  }
  getDGB010Val(para){
    if(para===undefined){
      return '';
    }
    return this.DGB010Map.get(para);
  }

  initDGB020Map(){
    this.supdic.getSFDic('DGB020').forEach(e=>{
      this.DGB020Map.set(e['value'],e['label']);
    });
  }
  getDGB020Val(para){
    if(para===undefined){
      return '';
    }
    return this.DGB020Map.get(para);
  }

  initDGB040Map(){
    this.supdic.getSFDic('DGB040').forEach(e=>{
      this.DGB040Map.set(e['value'],e['label']);
    });
  }
  getDGB040Val(para){
    if(para===undefined){
      return '';
    }
    return this.DGB040Map.get(para);
  }

  initDAD011Map(){
    this.supdic.getSFDic('DAD011').forEach(e=>{
      this.DAD011Map.set(e['value'],e['label']);
    });
  }
  getDAD011Val(para){
    if(para===undefined){
      return '';
    }
    return this.DAD011Map.get(para);
  }

  initDAD032Map(){
    this.supdic.getSFDic('DAD032').forEach(e=>{
      this.DAD032Map.set(e['value'],e['label']);
    });
  }
  getDAD032Val(para){
    if(para===undefined){
      return '';
    }
    return this.DAD032Map.get(para);
  }

  initDAD011TagMap(){
    this.DAD011TagMap.set('一件事','green');
    this.DAD011TagMap.set('行政许可','red');
    this.DAD011TagMap.set('行政审批','blue');
    this.DAD011TagMap.set('行政给付','orange');
  }

  getDAD011Tag(para){
    if(para===undefined){
      return '';
    }
    const temp=this.getDAD011Val(para);
    return this.DAD011TagMap.get(temp);
  }
  initDAD032BGMap(){
    this.DAD032BGMap.set('企业','success');
    this.DAD032BGMap.set('自然人','processing');
  }

  getDAD032Color(para){
    const temp=this.getDAD032Val(para);
    const res=this.DAD032BGMap.get(temp);
    return res;
  }

  initDAD040Map(){
    this.supdic.getSFDic('DAD040').forEach(e=>{
      this.DAD040Map.set(e['value'],e['label']);
    });
  }
  getDAD040Val(para){
    if(para===undefined){
      return '';
    }
    return this.DAD040Map.get(para);
  }

  initDAD173Map(){
    this.supdic.getSFDic('DAD173').forEach(e=>{
      this.DAD173Map.set(e['value'],e['label']);
    });
  }
  getDAD173Val(para){
    if(para===undefined){
      return '';
    }
    return this.DAD173Map.get(para);
  }

  initDAD175Map(){
    this.supdic.getSFDic('DAD173').forEach(e=>{
      this.DAD173Map.set(e['value'],e['label']);
    });
  }
  getDAD175Val(para){
    if(para===undefined){
      return '';
    }
    return this.DAD173Map.get(para);
  }

  getDAD168Val(para){
    if(para===undefined){
      return '';
    }
    if(para==='0'){
      return '不支持';
    }
    return '支持';
  }



  // initDAD065Map(){
  //   this.supdic.getSFDic('DAD065').forEach(e=>{
  //     this.DAD065Map.set(e['value'],e['label']);
  //   });
  // }
  // getDAD065Val(para){
  //   return this.DAD065Map.get(para);
  // }



  // initDAD012Map(){
  //   this.supdic.getSFDic('DAD012').forEach(e=>{
  //     this.DAD012Map.set(e['value'],e['label']);
  //   });
  // }
  // getDAD012Val(para){
  //   return this.DAD012Map.get(para);
  // }

/**************************获区数字汉字**************************** */
toChinesNum(num){
  let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; // changeNum[0] = "零"
  let unit = ["", "十", "百", "千", "万"];
  num = parseInt(num);
  let getWan = (temp) => {
  let strArr = temp.toString().split("").reverse();
  let newNum = "";
  for (var i = 0; i < strArr.length; i++) {
    newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
  }
   return newNum;
 }
 let overWan = Math.floor(num / 10000);
 let noWan = num % 10000+'';
 if (noWan.toString().length < 4) 
    noWan = "0" + noWan;
 return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
}

/********************************申报材料方法********************************** */
log(i:number): void {
  console.log(this.sxData.sbcl[i].sbclCheckGroup);
}
clxxCancel(){
  this.clxxGxVisible=false;
}

@ViewChild('clxxST',{static:false}) clxxST: STComponent;
clxxGxTextVisible=false;
listOfDataWg=[];
listOfDataYg=[];
showClSearchBtn=false;
// 材料查询
clcx(){
  // 拼接查询条件
  const resList=[];;
  let qxFlag=true;
  this.sxData.sbcl.forEach(clItem=>{
    let i=0;
    clItem.sbclCheckGroup.forEach(item=>{
      if(item.checked){
        i++;
        resList.push(item.value);
      }
    });
    if(i===0){
      qxFlag=false;
    }
  })
  // 校验是否全选
if(!qxFlag){
  this.msgSrv.error('还有条件未勾选');
  return;
}
  this.lbs.lbservice("ZSKCX_QBQCL",{para:{DAD017:this.DAD017,CKLIST:resList}}).then(resdata=>{
    if(resdata.code>0){
      this.listOfDataWg=[];
      this.listOfDataYg=[];
      this.listOfData=resdata.message.list;
      this.listOfData.forEach(item=>{
        if(item.DAD082==='0'){
          this.listOfDataWg.push(item);
          // flag=true;
        }else{
          this.listOfDataYg.push(item);
        }
      });
        this.clxxGxTextVisible=true;
        this.clxxVisible=true;
      // if(flag){// 存在不共享数据
      //   this.clxxGxTextVisible=true;
      //   this.clxxVisible=true;
      // }else{// 全部数据都共享
      //   this.clxxVisible=false;
      //   this.clxxGxTextVisible=true;
      // }
    }else{
      this.msgSrv.error(resdata.errmsg);
    }
  })
}

initClFirst(){
  this.lbs.lbservice("ZSKCX_QBQCL",{para:{DAD017:this.DAD017,CKLIST:[]}}).then(resdata=>{
    if(resdata.code>0){
      this.listOfDataWg=[];
      this.listOfDataYg=[];
      this.listOfData=resdata.message.list;
      this.listOfData.forEach(item=>{
        if(item.DAD082==='0'){
          this.listOfDataWg.push(item);
          // flag=true;
        }else{
          this.listOfDataYg.push(item);
        }
      });
        this.clxxGxTextVisible=true;
        this.clxxVisible=true;
    }else{
      this.msgSrv.error(resdata.errmsg);
    }
  })
}

// 材料重置
clcz(){
  this.sxData.sbcl.forEach(fi=>{
    fi.sbclCheckGroup.forEach(item=>{
      item.checked=false;
    });
  });
  this.clxxGxTextVisible=false;
  this.clxxVisible=false;
  this.clxxGxVisible=false;
}

showGxCl(){
  const resList=[];;
  // this.sxData.sbcl[i].sbclCheckGroup.forEach(item=>{
  //   if(item.checked){
  //     resList.push(item.value);
  //   }
  // });
  // this.clxxParam={DAD017:this.DAD017,CKLIST:resList};
  this.clxxGxVisible=true;
}

lcthandleCancel(){
  this.isShowLct=false;
}
showlc(){
 this.createComponentModal(this.DAD017,'aaa')

}

createComponentModal(id: number, name: string): void {
  const modal = this.modalService.create({
    nzTitle: '流程图',
    nzStyle: {
      top:'35px'
    },
    nzContent: FlowViewComponent,
    nzWidth: '1000px',
    nzMaskClosable: false,
    nzComponentParams: {
      id,
      name
    },
    nzOkText: null,
  });
}


/**********************************获取流程说明*********************************/

/****************************展示地图*********************************/

showMap(point:any){
  // if(point===undefined){
  //   this.msgSrv.error('渠道暂无坐标');
  //   return;
  // }else{
  //   this.showMapVisible=true;
  // }
}

showMapVisible=false;
showMapCancel(){
  this.showMapVisible=false;
}

}
